package com.bidevalution.service.impl;

import com.bidevalution.core.constant.ApiConstant;
import com.bidevalution.core.constant.CharConstant;
import com.bidevalution.core.enums.ApiEnum;
import com.bidevalution.dao.ApiAuditParamMapper;
import com.bidevalution.dao.ApiSectionInfoMapper;
import com.bidevalution.service.BidParamService;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.Md5Util;
import com.qtp.core.util.OConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huyiqi
 * @date 2019/11/16
 */
@Service(value = "bidParamService")
public class BidParamServiceImpl extends BaseServiceImpl implements BidParamService {

    private static final String AUDIT_PARAM_TDAUDIMETHODS = "tdAudimethods";
    private static final String AUDIT_PARAM_TDBUSINESS = "tdBusiness";
    private static final String AUDIT_PARAM_TDCOMPOSITE = "tdComposite";
    private static final String AUDIT_PARAM_TDTECHNOLOGY = "tdTechnology";
    private static final String AUDIT_PARAM_TDAUDITPARA = "tdAuditPara";
    private static final String AUDIT_PARAM_TDGRADEPARAM = "tdGradeParam";


    /**
     * 推送工程量清单
     * @param list 清单列表
     *
    xh:序号,
    SegmentShowId:项目编号,
    SegmentName:项目名称,
    Qdbh:清单编号,
    Qdmc:清单名称,
    Xmtz:项目特征,
    Jldw:计量单位,
    Gcl:工程量,
    Zhdj:综合单价

     * @return 返回调用结果
     */
    @Override
    public Response<String> sendList(List<Map<String, Object>> list) throws Exception {
        ApiEnum api = ApiEnum.SEND_LIST;
        String token = api.getToken();
        Map<String, Object> params = new HashMap<>(4);
        String json = GsonUtil.toJsonWithGson(list);
        params.put("gclqdJson", json);
        params.put("token", token);

        Map<String, Object> resultMap = invokeHttpApiPost(api, params);
        return t9ApiSuccess(resultMap);
    }

    /**
     * 获取评标参数
     * @param projectId 项目id
     * @param projectCode 项目编号
     * @return 返回调用结果
     */
    @Override
    public Response<String> getAuditParams(String projectId, String projectCode) throws Exception {
        ApiEnum api = ApiEnum.GET_AUDIT_PARAMS;
        String token = api.getToken();
        token = Md5Util.getMD5(projectId +","+ token);
        Map<String, String> params = new HashMap<>(8);
        params.put("projectID", projectId);
        params.put("projectCode", projectCode);
        params.put("token", token);

        Map<String, Object> resultMap = invokeHttpApi(api, params);
        if(resultMap != null) {
            setAudimethods(resultMap);
            setBusiness(resultMap);
            setComposite(resultMap);
            setTechnology(resultMap);
            setAuditPara(resultMap);
            setGradeParam(resultMap);
        }
        return Response.ok();
    }

    @Override
    public Response<String> getK1K2(String sectionCode) throws Exception {
        //调用接口
        ApiEnum api = ApiEnum.GET_K1K2;
        String token = api.getToken();
        Map<String, String> params = new HashMap<>(4);
        params.put("token", token);
        params.put("code", sectionCode);
        Map<String, Object> resultMap = invokeHttpApi(api, params);
        if(resultMap != null) {
            //判断是否成功
            String result = OConvertUtils.getString(resultMap.get("success"));
            if(CharConstant.CHAR_TRUE.equals(result)) {
                //获取项目id
                Map<String, Object> sectionInfo = sectionInfoMapper.getProjIdByCode(sectionCode);
                if(sectionInfo!= null) {
                    String projId = OConvertUtils.getString(sectionInfo.get("td_projectId"));
                    List<Map<String, Object>> list = new ArrayList<>();
                    //获取数据
                    String dataJson = OConvertUtils.getString(resultMap.get("data"));
                    if(isArrayJson(dataJson)) {
                        list = GsonUtil.jsonToListMapWithGson(dataJson);
                    } else if(isObjectJson(dataJson)) {
                        Map<String, Object> map = GsonUtil.jsonToMapWithGson(dataJson);
                        list.add(map);
                    }
                    if(list != null) {
                        for(Map<String, Object> para:list) {
                            String name;
                            String type = OConvertUtils.getString(para.get("paramType"));
                            if("1".equals(type)) {
                                name = "DownRate";
                            } else if("2".equals(type)) {
                                name = "K1";
                            } else {
                                name = "K2";
                            }
                            para.put("AUPAPROJECTID", projId);
                            para.put("AUPAPARATYPE", 2);
                            para.put("AUPAPARANAME", name);
                            para.put("AUPAPARAVALUE", OConvertUtils.getString(para.get("finalParamValue")));
                            auditParamMapper.insertAuditpara(para);
                        }
                        return Response.ok();
                    } else {
                        return Response.fail("参数列表为空");
                    }
                } else {
                    return Response.fail("无法根据标段编码查找项目id");
                }
            } else {
                return Response.fail("调用接口失败");
            }
        }
        return Response.fail("接口返回值为空");
    }

    @SuppressWarnings("unchecked")
    private void setAudimethods(Map<String, Object> resultMap) {
        Map<String, Object> map = (Map<String, Object>) resultMap.get(AUDIT_PARAM_TDAUDIMETHODS);

        auditParamMapper.insertAudimethods(map);
    }
    @SuppressWarnings("unchecked")
    private void setBusiness(Map<String, Object> resultMap) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) resultMap.get(AUDIT_PARAM_TDBUSINESS);
        if(list != null) {
            for(Map<String, Object> map:list) {
                auditParamMapper.insertBusiness(map);
            }
        }
    }
    @SuppressWarnings("unchecked")
    private void setComposite(Map<String, Object> resultMap) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) resultMap.get(AUDIT_PARAM_TDCOMPOSITE);
        if(list != null) {
            for(Map<String, Object> map:list) {
                auditParamMapper.insertComposite(map);
            }
        }
    }
    @SuppressWarnings("unchecked")
    private void setTechnology(Map<String, Object> resultMap) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) resultMap.get(AUDIT_PARAM_TDTECHNOLOGY);
        if(list != null) {
            for(Map<String, Object> map:list) {
                auditParamMapper.insertTechnology(map);
            }
        }
    }
    @SuppressWarnings("unchecked")
    private void setAuditPara(Map<String, Object> resultMap) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) resultMap.get(AUDIT_PARAM_TDAUDITPARA);
        if(list != null) {
            for(Map<String, Object> map:list) {
                auditParamMapper.insertAuditpara(map);
            }
        }
    }
    @SuppressWarnings("unchecked")
    private void setGradeParam(Map<String, Object> resultMap) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) resultMap.get(AUDIT_PARAM_TDGRADEPARAM);
        if(list != null) {
            for(Map<String, Object> map:list) {
                auditParamMapper.insertGradeParam(map);
            }
        }
    }

    @Autowired(required = false)
    private ApiAuditParamMapper auditParamMapper;

    @Autowired
    private ApiSectionInfoMapper sectionInfoMapper;
}
